#!/bin/bash

# ============================================
# 排查 Nginx 404 错误
# ============================================

echo "=== 排查 Nginx 404 错误 ==="
echo ""

# 1. 检查部署目录是否存在
echo "1. 检查部署目录..."
if [ -d "/var/www/frontend" ]; then
    echo "✓ /var/www/frontend 目录存在"
    ls -la /var/www/frontend | head -10
else
    echo "✗ /var/www/frontend 目录不存在！"
    echo "请先复制 dist 文件到部署目录"
fi
echo ""

# 2. 检查 index.html 是否存在
echo "2. 检查 index.html..."
if [ -f "/var/www/frontend/index.html" ]; then
    echo "✓ index.html 存在"
    ls -lh /var/www/frontend/index.html
else
    echo "✗ index.html 不存在！"
    echo "这是问题所在，需要重新复制文件"
fi
echo ""

# 3. 检查 Nginx 配置
echo "3. 检查 Nginx 配置..."
if [ -f "/etc/nginx/conf.d/frontend.conf" ]; then
    echo "✓ frontend.conf 存在"
    echo "配置文件中的 root 路径："
    grep "root" /etc/nginx/conf.d/frontend.conf | head -1
else
    echo "✗ frontend.conf 不存在"
fi
echo ""

# 4. 检查文件权限
echo "4. 检查文件权限..."
ls -ld /var/www/frontend
echo ""

# 5. 检查 Nginx 错误日志
echo "5. 最近的 Nginx 错误日志："
sudo tail -20 /var/log/nginx/error.log 2>/dev/null || echo "无法读取错误日志"
echo ""

# 6. 检查访问日志
echo "6. 最近的访问日志："
sudo tail -5 /var/log/nginx/frontend_access.log 2>/dev/null || echo "无法读取访问日志"
echo ""

echo "=== 修复建议 ==="
echo "如果 index.html 不存在，执行："
echo "  sudo cp -r /root/OCR_CER_AUIDT_ADMIN/django-vue3-admin/web/dist/* /var/www/frontend/"
echo "  sudo chown -R root:root /var/www/frontend"
echo "  sudo chmod -R 755 /var/www/frontend"
echo "  sudo systemctl reload nginx"

